import { Scene, sys } from "cc";
import { EventEnum, ItemStatusEnum, ItemTypeEnum, SceneEnum, TriggerStatusEnum } from "../../Enum";
import Singleton from "../Base/Singleton";
import EventManager from "./EventManager";


interface Item {
    status: ItemStatusEnum;
    type: ItemTypeEnum
}
const STORAGE_KEY = 'STORAGE_KEY';

export default class DataManager extends Singleton {
    private _currItemType: ItemTypeEnum | null = null;
    private _isSelect: Boolean = false;
    private _mailBoxStatus: TriggerStatusEnum = TriggerStatusEnum.Pengind
    private _grandMaStatus: TriggerStatusEnum = TriggerStatusEnum.Pengind
    private _grandmaDialogIndex = -1
    private _curScene: SceneEnum = SceneEnum.H1;

    static get Instance() {
        return super.GetInstance<DataManager>();
    }

    private _items: Array<Item> = [
        { type: ItemTypeEnum.KEY, status: ItemStatusEnum.Scene },
        { type: ItemTypeEnum.MAIL, status: ItemStatusEnum.DISABLED }
    ]

    get curScene() {
        return this._curScene;
    }

    set curScene(newData: SceneEnum) {
        this._curScene = newData
        this.renderAndSave();
    }

    get grandmaDialogIndex() {
        return this._grandmaDialogIndex;
    }
    set grandmaDialogIndex(newData) {
        this._grandmaDialogIndex = newData;
        this.renderAndSave();
    }

    get grandMaStatus() {
        return this._grandMaStatus;
    }
    set grandMaStatus(newData: TriggerStatusEnum) {
        this._grandMaStatus = newData;
        this.renderAndSave();
    }


    get mailBoxStatus() {
        return this._mailBoxStatus;
    }

    set mailBoxStatus(newData: TriggerStatusEnum) {
        this._mailBoxStatus = newData;
        this.renderAndSave();
    }

    get isSelect() {
        return this._isSelect;
    }

    set isSelect(newData: Boolean) {
        this._isSelect = newData;
        this.renderAndSave();
    }

    get currItemType() {
        return this._currItemType;
    }

    set currItemType(ItemData: ItemTypeEnum) {
        this._currItemType = ItemData;
        this.renderAndSave();
    }

    get items() {
        return this._items;
    }

    set items(newData) {
        this._items = newData;

        //触发渲染
        this.renderAndSave();
    }

    renderAndSave() {
        EventManager.Instance.emit(EventEnum.Render)

        sys.localStorage.setItem(STORAGE_KEY, JSON.stringify({
            currItemType: this.currItemType,
            isSelect: this.isSelect,
            items: this.items,
            mailBoxStatus: this.mailBoxStatus,
            grandMaStatus: this.grandMaStatus,
            grandmaDialogIndex: this.grandmaDialogIndex,
            curScene: this.curScene
        }))
    }

    restore() {
        const _data = sys.localStorage.getItem(STORAGE_KEY);
        try {
            const data = JSON.parse(_data);
            this.currItemType = data.currItemType;
            this.isSelect = data.isSelect;
            this.items = data.items;
            this.mailBoxStatus = data.mailBoxStatus;
            this.grandMaStatus = data.grandMaStatus;
            this.grandmaDialogIndex = data.grandmaDialogIndex;
            this.curScene = data.curScene;
        } catch (e) {
            this.reset();
        }
    }

    reset() {
        this.currItemType = null;
        this.isSelect = false;
        this.items = [
            { type: ItemTypeEnum.KEY, status: ItemStatusEnum.Scene },
            { type: ItemTypeEnum.MAIL, status: ItemStatusEnum.DISABLED }
        ];
        this.mailBoxStatus = TriggerStatusEnum.Pengind;
        this.grandMaStatus = TriggerStatusEnum.Pengind;
        this.grandmaDialogIndex = -1;
        this.curScene = SceneEnum.H1;
    }
}



